package com.odontosis.util;

import java.sql.Connection;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

	public static  SessionFactory sessionFactory;
	
	public static Session sessao;

	static {
		try {			
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		} catch (Throwable ex) {
			// Loga a exce��o. Caso contr�rio ela ser� esquecida (ao falhar).
			System.err.println("A cria��o da SessionFactory inicial falhou."
					+ ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static final ThreadLocal<Session> session = new ThreadLocal<Session>();

	public static Session currentSession() throws HibernateException {
		/*Session s = (Session) session.get();
//		// Abre uma nova sess�o caso a Thread atual n�o tenha nenhuma
//		if (s == null) {
		s = sessionFactory.openSession();
		// Guarda-o na vari�vel ThreadLocal
		s.setCacheMode(CacheMode.IGNORE);
		s.clear();
		session.set(s);
//		}*/
        if(sessao == null) {  
            SessionFactory sessionFactory = null;  
  
           // Get the session factory we can use for persistence  
           sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();  
           sessionFactory.evictQueries();  
           // Ask for a session using the JDBC information we've configured  
           sessao = sessionFactory.openSession();  
           sessao.setFlushMode(FlushMode.ALWAYS);  
        }  
        return sessao;
	}

	public static Connection getConnection() throws HibernateException {
		return currentSession().connection();
	}

	public static void closeSession() throws HibernateException {
		Session s = (Session) session.get();
		if (s != null)
			s.close();
		session.set(null);
		//sessao.close();
	}
	/*  
		     static Session session = null;  
		     static Configuration configuration = null;  
		       
		     static {  
		        System.out.println("Criando Hibernate Config");  
		          
		        String aStr = SystemConfiguration.getInstance().getProperty("hibernateConfigFile" , "../Hibernate/config/hibernate.cfg.xml");  
		          
		        System.out.println("Criando Hibernate Config a partir de " + aStr);  
		       File aFile = new File(aStr);  
		       System.out.println("arquivo: " + aFile);  
		       configuration = new Configuration().configure(aFile);  
		       System.out.println("criei a configuração ");  
		     }  
		   
		 public static Session getSession() throws Exception {  
		         if(session == null) {  
		             SessionFactory sessionFactory = null;  
		   
		            // Get the session factory we can use for persistence  
		            sessionFactory = getConfiguration().buildSessionFactory();  
		            sessionFactory.evictQueries();  
		            // Ask for a session using the JDBC information we've configured  
		            session = sessionFactory.openSession();  
		            session.setFlushMode(FlushMode.ALWAYS);  
		         }  
		         return session;  
		   
		     }  
		 }*/  
	

}
